package com.iss.supermarket.MainPage;

import com.iss.supermarket.EntryUI.EntryUI;
import com.iss.supermarket.GoodsInfoManagerUI.GoodsInfoManagerUI;
import com.iss.supermarket.LoginUI.LoginUI;
import com.iss.supermarket.OutFlowUI.OutFlowUI;
import com.iss.supermarket.PersonalInfoManageUI.PersonInfoUI;
import com.iss.supermarket.ReturnGoodsUI.ReturnGoodsUI;
import com.iss.supermarket.TradeUI.TradeUI;
import com.iss.supermarket.UserMangerUI.UserManagerUI;
import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.SpUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class MainPage extends JFrame {
    //单例模式
    private static MainPage mainPage;
    //定义组件
    private Container container;//container在后续中(不仅在InitUI中)所以定义在成员变量部分
    private JMenuBar jMenuBar;
    private JMenu jMenu1;
    private JMenu jMenu2;
    private JMenuItem[] jMenuItems;
    private JPanel ContentPanel;
    private SpUser thisUser;

    public MainPage(SpUser spUser) {
        super("超市管理系统");

        thisUser = spUser;
        //***测试***//
        thisUser.setRemark("管理员");
        InitAssembly();
        InitUI(thisUser);
    }

    private void InitUI(SpUser spUser) {
        container = getContentPane();
        JPanel jPanel1 = new JPanel();

        jMenu1.add(jMenuItems[0]);
        jMenu1.add(jMenuItems[1]);
        jMenu1.add(jMenuItems[2]);
        jMenu1.add(jMenuItems[3]);
        jMenu1.add(jMenuItems[4]);
        jMenu2.add(jMenuItems[5]);
        jMenu2.add(jMenuItems[6]);
        jMenu2.add(jMenuItems[7]);

        if("管理员".equals(spUser.getRemark())){
            jMenu2.add(jMenuItems[8]);
        }
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);
        jPanel1.add(jMenuBar);
        container.add(jMenuBar, BorderLayout.NORTH);
        setVisible(true);
        setSize(1000, 800);

        container.add(ContentPanel, BorderLayout.CENTER);
        setComponentInCenter(this);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    private void InitAssembly(){
        mainPage = this;

        jMenuBar = new JMenuBar();
        jMenuBar.setPreferredSize(new Dimension(500,40));

        jMenu1 = new JMenu("业务管理");
        jMenu1.setFont(new Font("黑体",Font.PLAIN,14));
        jMenu2 = new JMenu("系统管理");
        jMenu2.setFont(new Font("黑体",Font.PLAIN,14));

        jMenuItems = new JMenuItem[9];
        //形如KeyEvent.VK_T为触发对应菜单项的键码值
        jMenuItems[0] = new JMenuItem("商品交易(T)", KeyEvent.VK_T);
        //添加菜单项的快捷键
        jMenuItems[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[1] = new JMenuItem("入库管理(I)", KeyEvent.VK_I);
        jMenuItems[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[2] = new JMenuItem("出库管理(O)", KeyEvent.VK_O);
        jMenuItems[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[3] = new JMenuItem("退货管理(R)", KeyEvent.VK_R);
        jMenuItems[3].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[4] = new JMenuItem("商品信息管理(G)", KeyEvent.VK_G);
        jMenuItems[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[5] = new JMenuItem("个人信息(N)", KeyEvent.VK_N);
        jMenuItems[5].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[6] = new JMenuItem("注销(C)", KeyEvent.VK_C);
        jMenuItems[6].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[7] = new JMenuItem("退出(E)", KeyEvent.VK_E);
        jMenuItems[7].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.ALT_DOWN_MASK));

        jMenuItems[8] = new JMenuItem("用户管理(U)", KeyEvent.VK_U);
        jMenuItems[8].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.ALT_DOWN_MASK));

        for(int i=0;i<jMenuItems.length;++i){
            jMenuItems[i].setFont(new Font("黑体",Font.PLAIN,13));
            jMenuItems[i].addActionListener(new MenuAction());
        }

        ContentPanel = new JPanel();
    }

    private class MenuAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //删除container容器中的原有的功能页面，存放新的功能页面
            container.remove(((BorderLayout) container.getLayout()).getLayoutComponent(BorderLayout.CENTER));
            //获得用户权限
            String role = thisUser.getRemark();

            //检查用户权限，若用户拥有对应的权限，则生成对应的窗口
            if("商品交易(T)".equals(e.getActionCommand()) && ("收银员".equals(role) || "管理员".equals(role))){
                //传递用户信息，因为单例方法在NetWork类中有使用，所以使用静态方法传递用户信息
                TradeUI.setThisUser(thisUser);
                TradeUI tradeUI = TradeUI.getTradeInfoUI();
                ContentPanel = tradeUI.getTradePanel();

            }else if("入库管理(I)".equals(e.getActionCommand()) && ("仓库管理员".equals(role) || "管理员".equals(role))){
                EntryUI entryUI = EntryUI.getEntryUI();
                ContentPanel = entryUI.getEntryPanel();

            }else if("出库管理(O)".equals(e.getActionCommand()) && ("仓库管理员".equals(role) || "管理员".equals(role))){
                OutFlowUI outFlowUI = OutFlowUI.getOutFlowUI();
                ContentPanel = outFlowUI.getOutFlowPanel();

            }else if("商品信息管理(G)".equals(e.getActionCommand()) && ("仓库管理员".equals(role) || "管理员".equals(role))){
                GoodsInfoManagerUI goodsInfoManagerUI = GoodsInfoManagerUI.getGoodsInfoManageUI();
                ContentPanel = goodsInfoManagerUI.getGoodsInfoManagePanel();

            }else if("退货管理(R)".equals(e.getActionCommand()) && ("售后管理员".equals(role) || "管理员".equals(role))){
                ReturnGoodsUI returnGoodsUI = ReturnGoodsUI.getReturnGoodsUI();
                ContentPanel = returnGoodsUI.getReturnGoodsPanel();
                //退货管理功能尺寸较大，所以调整主页面的大小使页面更好的显示
                mainPage.setSize(ContentPanel.getPreferredSize());
                setComponentInCenter(mainPage);

            }else if("个人信息(N)".equals(e.getActionCommand())){
                PersonInfoUI personInfoUI = PersonInfoUI.getPersonInfoUI(thisUser);
                ContentPanel = personInfoUI.getPersonInfoUIPanel();

            }else if("注销(C)".equals(e.getActionCommand())){
                mainPage.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                LoginUI loginUI = new LoginUI();
                mainPage = null;
                dispose();

            }else if ("退出(E)".equals(e.getActionCommand())){
                System.exit(0);

            }else if ("用户管理(U)".equals(e.getActionCommand())){
                UserManagerUI userManagerUI = UserManagerUI.getUserManagerUI();
                ContentPanel = userManagerUI.getUserManagerPanel();
                //用户管理功能尺寸较大，所以调整主页面的大小使页面更好的显示
                mainPage.setSize(ContentPanel.getPreferredSize());
                setComponentInCenter(mainPage);

            }else{
                //用户权限不满足，返回提示信息
                MsgWin msgWin = new MsgWin("无权限");
            }

            //添加功能页面，并且刷新显示
            container.add(ContentPanel, BorderLayout.CENTER);
            container.repaint();
            container.validate();
        }
    }

    public static synchronized MainPage getMainPage(SpUser spUser) {
        if (mainPage == null) {
            mainPage = new MainPage(spUser);
        }
        return mainPage;
    }

    private void setComponentInCenter(Component component){
        Dimension UISIZAE = component.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (UISIZAE.height > screenSize.height)
            UISIZAE.height = screenSize.height;
        if (UISIZAE.width > screenSize.width)
            UISIZAE.width = screenSize.width;
        component.setLocation((screenSize.width - UISIZAE.width) / 2, (screenSize.height - UISIZAE.height) / 2);
    }
}